/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;
import java.time.LocalDate;

import cn.webrx.cn.webrx.entit.Book;
import cn.webrx.mapper.BookMapper;
import cn.webrx.mapper.StudentMapper;
import cn.webrx.mybatis.interceptor.MyPager;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Config;
import tk.mybatis.mapper.mapperhelper.MapperHelper;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * <p>Project: mybatis2021 - MapperTest
 * <p>Powered by webrx On 2021-07-15 14:53:51
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
public class MapperTest {
    public static void main(String[] args) throws IOException {
        //建立数据源，
        DruidDataSource ds = new DruidDataSource();
        Properties prop = new Properties();
        prop.load(Resources.getResourceAsReader("db.properties"));
        ds.setConnectProperties(prop);

        TransactionFactory tx = new JdbcTransactionFactory();
        Environment env = new Environment("development", tx, ds);
        Configuration cfg = new Configuration(env);

        cfg.addMappers("cn.webrx.mapper");

        //注册通用mapper
        MapperHelper mh = new MapperHelper();
        mh.registerMapper(Mapper.class);
        mh.processConfiguration(cfg);

        //注册分页拦截器插件
        PageInterceptor pp = new PageInterceptor();
        cfg.addInterceptor(pp);


        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);
        SqlSession ss = sf.openSession();

        BookMapper bm = ss.getMapper(BookMapper.class);

        /*
        MapperHelper mh = new MapperHelper();
        //特殊配置
        Config config = new Config();
        //具体支持的参数看后面的文档
        //config.setXXX(XXX);
        //设置配置
        mh.setConfig(config);
        // 注册自己项目中使用的通用Mapper接口，这里没有默认值，必须手动注册
        mh.registerMapper(Mapper.class);
        //配置完成后，执行下面的操作
        mh.processConfiguration(ss.getConfiguration());

         */

        //通用Mapper 插入数据
        Book book = new Book();
        book.setName("《java开发入门》");
        book.setPrice(58.0D);
        book.setPublish(LocalDate.now());

        int row = bm.insert(book);
        System.out.println(row);

        //查询数据
        PageHelper.startPage(1, 1);
        List<Book> list = bm.selectAll();
        System.out.println(list);

        //Book bk1 = bm.selectByPrimaryKey(2);
        //System.out.println(bk1);

        //bk1.setPrice(88.8);
        //修改数据
        //int num = bm.updateByPrimaryKey(bk1);
        //System.out.println(num);

        //删除数据
        int i = bm.deleteByPrimaryKey(2);
        System.out.println(i);
        int n = bm.deleteByPrimaryKey(new Book(2));
        System.out.println(n);

        int a = bm.delete(new Book(2));
        System.out.println(a);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("id",2);
        int c = bm.deleteByPrimaryKey(map);
        System.out.println(c);

        ss.commit();

    }
}
